#include<head.h>
#include "threadpool.h"
#include "passwordCheck.h"

void getSalt(char *str)
{
    int i, flag;
    srand(time(NULL));
    for(i = 0; i < 8; i++){
        flag = rand()%3;
        switch(flag){
        case 0:
            str[i] = rand()%26 + 'a';
            break;
        case 1:
            str[i] = rand()%26 + 'A';
            break;
        case 2:
            str[i] = rand()%10 + '0';
            break;
        }
    }
}

int func_passwdCheck(int peerfd)
{
    char buff[1024] = {0};

    /*----数据库对应结构体-----*/
    typedef struct user_file 
    {
        int id;
        char username[20];
        char salt[20];
        char cryptpasswd[200];
        char pwd[200];
    }user_infor;

    /*------连接到数据库-----*/
    MYSQL *conn = NULL;
    char *host = "localhost";
    char *user = "root";
    char *passwd1= "1234";
    char *db = "Project1";

    /*----初始化mysql连接句柄----*/
    conn = mysql_init(conn);

    /*------建立连接-----*/
    if(mysql_real_connect(conn, host, user, passwd1, db, 0, NULL,0) == NULL)
    {
        char no_1[] = "---服务器连接不成功，请重新连接---";
        send(peerfd, no_1, sizeof(no_1), 0);
        return -1;
    }
    user_infor us1;
    char username[20] = {0};
    char salt[11] = {0};
    char passwd[200] = {0};
    int length = 0;
    train_t train;

    recv(peerfd, buff, sizeof(int), 0);
    int flag = buff[0] - '0';
    if(flag == 0)
    {
        /*----接收用户名----*/
        int length = 0;
        recvCircle(peerfd, (char *)&length, 4);
        recvCircle(peerfd, username, length);

        strcpy(us1.username, username);
        us1.username[strlen(us1.username)-1]='\0';
        printf("us1.username:%s\n", us1.username);//看一下确保正确

        /*----生成盐值并发送----*/
        char rdmstr[8] = {0};
        getSalt(rdmstr);//得到8个随机数
        char t[12] = "$6$";
        strncat(t,rdmstr,8);//拼接得到正确盐值
        puts(t);
        strcpy(us1.salt,t);
        train.length = strlen(us1.salt);
        strcpy(train.buff, us1.salt);
        send(peerfd, &train, 4 + train.length, 0);

        /*----接收密文----*/
        length = 0;
        recvCircle(peerfd, (char *)&length, 4);
        recvCircle(peerfd, passwd, length);
        printf("接收到的密文：%s\n", passwd);

        strcpy(us1.cryptpasswd, passwd);
        strcpy(us1.pwd, "/server/");

        /*----sql的执行语句----*/
        char insertUser[1024] = {0};
        sprintf(insertUser,"insert into userpasswd (username, salt, cryptpasswd, pwd) values('%s','%s','%s','%s');", us1.username, us1.salt, us1.cryptpasswd, us1.pwd);
        printf("插入的sql语句为：%s\n", insertUser);

        /*----执行sql语句----*/
        int ret = mysql_query(conn, insertUser);
        if(ret!=0)
        {
            printf("error query:%s\n",mysql_error(conn));
        }
        printf("%d\n",ret);
        if(ret == 0){
            /*----再查找一下，看看是否插入成功----*/
            char find[1024] = "select * from userpasswd where username = ";
            sprintf(find,"%s'%s';",find,us1.username);
            puts(find);
            mysql_query(conn, find);
            MYSQL_RES *result = mysql_store_result(conn);
            if(result !=  NULL){
                char yes[] = "注册成功！请登陆。";
                send(peerfd, yes, sizeof(yes), 0);
            }
            else{
                char no[] = "注册失败！请重新接入服务器。";
                send(peerfd, no, sizeof(no), 0);
                exit(-1);
            }
        }
        else{
            char no[] = "注册失败！请重新接入服务器。";
            send(peerfd, no, sizeof(no), 0);
            exit(-1);
        }
        return 1;
    }
    else if(flag == 1) {
        /*----接收用户名----*/
        length = 0;
        recvCircle(peerfd, (char *)&length, 4);
        recvCircle(peerfd, username, length);
        username[strlen(username)-1]='\0';
        /*----找到对应用户的盐值并发送----*/
        char findUs1salt[1024] = {0};
        sprintf(findUs1salt, "select salt from userpasswd where username ='%s';", username);
        //执行查找语句，不管找没找到返回值都是0
        puts(findUs1salt);
        int ret = mysql_query(conn, findUs1salt);
        printf("ret:%d\n",ret);
        if(ret != 0){
            printf("error: %s\n", mysql_error(conn));
            exit(-1);
        }
        MYSQL_RES *usalt = mysql_store_result(conn);
        if(usalt==NULL)
        {
            printf("error query2:%s\n",mysql_error(conn));
            while(1);
        }
        MYSQL_ROW row=mysql_fetch_row(usalt);

        memset(&us1,0,sizeof(us1));
        strcpy(us1.salt, row[0]);
        puts(us1.salt);
        train.length = strlen(us1.salt);
        strcpy(train.buff, us1.salt);
        send(peerfd, &train, 4 + train.length, 0);


        /*----接收密文----*/
        length = 0;
        recvCircle(peerfd, (char *)&length, 4);
        recvCircle(peerfd, passwd, length);
        printf("接收到的密文：%s\n", passwd);

        /*----查询用户的密文进行对比----*/
        char findUs1cryptpasswd[1024] = "select cryptpasswd from userpasswd where username = ";
        sprintf(findUs1cryptpasswd, "%s'%s'", findUs1cryptpasswd,username);
        //执行查询语句
        ret = mysql_query(conn, findUs1cryptpasswd);
        if(ret != 0){
            printf("error: %s\n", mysql_error(conn));
            exit(-1);
        }
        MYSQL_RES *ucryptpasswd = mysql_store_result(conn);
        row=mysql_fetch_row(ucryptpasswd);
        strcpy(us1.cryptpasswd, row[0]);

        /*----将两个密文进行对比----*/
        if(strcmp(us1.cryptpasswd, passwd) == 0){
            char success[] = "登陆成功！";
            send (peerfd, success, strlen(success), 0);

        }
        else {
            char e[] = "登录失败！请重新登陆！";
            send (peerfd, e, strlen(e), 0);
            return 1;
        }
        return 0;
    }

}
